package cn.pid21.client.request;

import cn.pid21.client.exception.PidClientException;
import cn.pid21.client.model.ParamType;
import cn.pid21.client.model.PidService;
import cn.pid21.client.util.RequestBodyUtil;
import cn.pid21.client.util.RequestUtil;
import lombok.Getter;
import okhttp3.Headers;
import okhttp3.Request;

/**
 * PidRegisterRequest
 *
 * @author 郭志斌
 * @version 1.0.0
 * @date 2019/8/16 15:33
 */
@Getter
public class PidRegisterRequest implements PidRequest{

    /**
     * 注册参数
     */
    private String params;

    /**
     * 参数类型 json/xml
     */
    private ParamType paramType;

    private PidRegisterRequest(){}

    public static PidRegisterRequest builder(){
        return new PidRegisterRequest();
    }

    public PidRegisterRequest params(String params){
        this.params = params;
        return this;
    }

    public PidRegisterRequest paramType(ParamType paramType){
        this.paramType = paramType;
        return this;
    }

    @Override
    public Request build(String clientId,String secret) {
        Headers headers = RequestUtil.headers(PidRequestHeader.builder().clientId(clientId).secret(secret));
        switch (paramType){
            case JSON:
                return RequestUtil.post(headers, RequestBodyUtil.json(params),service().getName());
            case XML:
                return RequestUtil.post(headers, RequestBodyUtil.xml(params),service().getName());
            default:
                throw new PidClientException("请求参数类型异常");
        }
    }

    @Override
    public PidService service() {
        return PidService.PID_REGISTER_SERVICE;
    }
}
